using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

namespace EntityKit.Runtime
{
    public static partial class AudioKit
    {
        /// <summary>
        /// 环境音音量
        /// </summary>
        public static float AmbientVolume
        {
            get => ambientVolume;
            set
            {
                if (ambientSource != null) ambientSource.volume = ambientVolume = Mathf.Clamp01(value);
            }
        }

        public static bool IsAmbientPlaying => ambientSource != null && ambientSource.isPlaying;

        /// <summary>
        /// 环境音静音控制
        /// </summary>
        public static bool AmbientMute
        {
            get => isAmbientMute;
            set
            {
                if (isAmbientMute == value) return;
                isAmbientMute = value;
                if (ambientSource != null) ambientSource.mute = value;
            }
        }

        /// <summary>
        /// 播放环境音
        /// </summary>
        /// <param name="clip">音频剪辑</param>
        /// <param name="isLoop">是否循环</param>
        /// <param name="speed">播放速度</param>
        public static void PlayAmbientSound(AudioClip clip, bool isLoop = true, float speed = 1)
        {
            if (!isInitialized || !ValidateClip(clip)) return;

            ConfigureAudioSource(ambientSource, clip, isLoop, speed);
            if (ambientSource != null)
            {
                ambientSource.Play();
            }
        }

        /// <summary>
        /// 停止播放环境音
        /// </summary>
        public static void StopAmbientSound()
        {
            if (ambientSource != null)
            {
                ambientSource.Stop();
            }
        }

        /// <summary>
        /// 暂停播放环境音
        /// </summary>
        public static void PauseAmbientSound()
        {
            if (ambientSource != null)
            {
                ambientSource.Pause();
            }
        }

        /// <summary>
        /// 恢复播放环境音
        /// </summary>
        public static void ResumeAmbientSound()
        {
            if (ambientSource != null)
            {
                ambientSource.UnPause();
            }
        }

        private static void AmbientSoundInitialize()
        {
            ambientSource = CreateAudioSource("AmbientSource", ambientVolume, 1, 0, root, isAmbientMute);
            if (ambientSource != null)
            {
                ambientSource.loop = true;
                AmbientVolume = ambientVolumeDefault;
            }
        }

        private static void AmbientSoundUpdate()
        {
        }

        private static void AmbientSoundRelease()
        {
            if (ambientSource != null)
            {
                Object.Destroy(ambientSource.gameObject);
                ambientSource = null;
            }
        }

        private static AudioSource ambientSource;
        private static float ambientVolumeDefault = 0.6f;
        private static float ambientVolume;
        private static bool isAmbientMute;
    }
}